import TypeDiagnose from '@/framework/infra/type/TypeDiagnose'

export default class DomUtil {
  static invokeClosetDomWithFuncName(dom, funcName, arg = undefined) {
    if (!dom) {
      throw new Error(`closet中找不到${funcName}的实现`)
    }
    const func = dom[funcName]
    if (func && TypeDiagnose.isFunction(func)) {
      return func.call(dom, arg)
    }
    return this.invokeClosetDomWithFuncName(dom.$parent, funcName, arg)
  }

  static findClosetFuncHandleWithFuncName(dom, funcName) {
    if (!dom) {
      return null
    }
    const func = dom[funcName]
    if (func && TypeDiagnose.isFunction(func)) {
      return func.bind(dom)
    }
    return this.findClosetFuncHandleWithFuncName(dom.$parent, funcName)
  }
}
